/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月29日         luwei
 */

package com.coocaa.launcher.sidebar.left.item.speedup;

import android.content.Context;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;

import com.coocaa.launcher.framework.sidebar.item.Item;
import com.coocaa.launcher.framework.sidebar.item.ItemView;
import com.coocaa.launcher.sidebar.left.item.speedup.layout.SpeedUpLayout;
import com.coocaa.x.framework.utils.AsyncTask;
import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.R;
import com.tianci.appstore.utils.SystemUtils;

public class SpeedUp extends Item {
    private boolean mStart = true;
    private boolean mIsSpeedUp = false;
    private long mMemoryTotal = 0L;
    private SpeedUpLayout speedUpLayout = null;
    private int persent = 0;
    private String mem = "";
    private float part = 0f;

    public SpeedUp(Context context) {
        super(context);
        beginGetMemory();
    }

    private void beginGetMemory() {
        mMemoryTotal = SystemUtils.getmem_TOLAL();
        part = mMemoryTotal / 1024f / 100f;
//        new Thread(new Runnable()
//        {
//            @Override
//            public void run()
//            {
//                while (mStart)
//                {
//                    if (!mIsSpeedUp)
//                    {
//                        ((SpeedUpItem) mItemView).updateMemory(getMemoryPercentage() + "%");
//                    }
//                    try
//                    {
//                        Thread.sleep(4000);
//                    } catch (InterruptedException e)
//                    {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
//                }
//            }
//        }).start();
    }

    private int getMemoryPercentage() {
        long used = SystemUtils.getmem_UNUSED(mContext);
        float percentage = used * 100 / mMemoryTotal;
        return 100 - (int) percentage;
    }

    @Override
    public void onItemFocusChange(View v, boolean focus) {
        // TODO Auto-generated method stub
        super.onItemFocusChange(v, focus);
    }

    @Override
    public boolean onItemKeyEvent(View v, int keycode, KeyEvent keyEvent) {
        // TODO Auto-generated method stub
        return mIsSpeedUp;
    }

    @Override
    public void onItemClick(View v) {
        if (!mIsSpeedUp) {
            mIsSpeedUp = true;
            AsyncTask.runOnThread(new Runnable() {
                @Override
                public void run() {
                    int start = getMemoryPercentage();
                    SystemUtils.killAll(mContext);
                    int end = getMemoryPercentage();
                    persent = (start - end);
                    if (persent < 0)
                        persent = 0;
                    mem = (start - end) * mMemoryTotal / 1024 / 100 + "MB";
                }
            });
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            if (speedUpLayout == null) {
                speedUpLayout = new SpeedUpLayout(mContext);
            }
            mExtraViewListener.addExtraView(params, speedUpLayout, getHideAnim());
            speedUpLayout.reSet();
            super.onItemClick(v);
        }

    }


    private Animation getShowAnim() {
        Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.speed_side_show_anim);
        anim.setFillAfter(true);
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                ((SpeedUpItem) mItemView).endSpeedUp();
                mItemView.requestFocus();
                mIsSpeedUp = false;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        return anim;
    }

    private Handler mHandler = new Handler();

    private int num = -1;

    public int getPresent() {
        num++;
        switch (num % 5) {
            case 0:
                return 0;
            case 1:
                return 4;
            case 2:
                return 15;
            case 3:
                return 2;
            case 4:
                return 8;
        }
        return 0;
    }

    private Animation getHideAnim() {
        Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.speed_side_hide_anim);
        anim.setFillAfter(true);
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (persent > 0) {
                            if (persent > 5) {
                                for (int i = 1; i <= 4; i++) {
                                    final int j = i;
                                    mHandler.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            speedUpLayout.updateSpeedupResult((persent - 4 + j) + "%",
                                                    (int) ((persent - 4 + j) * part) + "MB");
                                        }
                                    }, 1500 + 300 * j);
                                }
                            } else {
                                for (int i = 1; i <= persent; i++) {
                                    final int j = i;
                                    mHandler.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            LogUtils.i("total", "memory is " + mMemoryTotal + " " + part);
                                            speedUpLayout.updateSpeedupResult(j + "%",
                                                    (int) (j * part) + "MB");
                                        }
                                    }, 1500 + 300 * j);
                                }
                            }
                        } else
                            speedUpLayout.updateSpeedupResult(persent + "%", mem);
                    }
                }, 500);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                speedUpLayout.startAnimation();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        speedUpLayout.stopAnimation();
                        mExtraViewListener.removeExtraVeiw(speedUpLayout, getShowAnim());
                    }
                }, 5000);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        return anim;
    }


    @Override
    public ItemView createItemView() {
        return new SpeedUpItem(mContext);
    }
}
